import axios from '/@/api/axios'
import { getRoleCode, getUserByParent } from '/@/util/business-util'

// 根据待办类型获取待办已完成/未完成的数量
export let getTodoTotalByType = async function (
  type: string,
  userInfo: any,
  curConditions: any
) {
  let qtyUrl = '' // 获取未完成/已完成数量的url
  let params = getParmas(type, curConditions) // 获取通用参数
  params['tenantId'] = userInfo.tenantId
  params['specialTypeId'] = 1
  let undoneKey = '' // 未完成key
  let doneKey = '' // 已完成key
  switch (type) {
    case 'evaluation': // '学生测评'
      let roleCode = getRoleCode(userInfo)
      // 学生或者家长
      if (roleCode === 'student' || roleCode === 'parent') {
        qtyUrl =
          '/api/blade-measure/measure/projectgaugeincludeuser/findEvaluationTotal'
        undoneKey = 'wcTotal'
        doneKey = 'ycTotal'
      } else {
        // 老师
        qtyUrl = '/api/blade-measure/measure/usersummary/findAllTestGroupTotal'
        undoneKey = 'noTestSum'
        doneKey = 'testSum'
      }
      break
    case 'warn': // '学生预警'
      qtyUrl = '/api/blade-measure/measure/report/findUserWarnTotal'
      undoneKey = 'wcl'
      doneKey = 'ycl'
      break
    case 'reserve': // '学生预约'
      qtyUrl = '/api/blade-measure/measure/report/findUserWarnTotal' // ---------------测试数据---------------------------------
      break
  }
  let { data } = await axios({
    url: qtyUrl,
    method: 'get',
    params,
  })
  return {
    undoneQty: data.success ? data.data[undoneKey] || 0 : 0, // 未完成的数量
    doneQty: data.success ? data.data[doneKey] || 0 : 0, // 已完成的数量
  }
}
// 根据待办类型获取待办详情
export let getTodoDetails = async function (
  commit: any,
  state: any,
  type: any,
  curConditions: any,
  doneType: any,
  pager: any
) {
  let params = getParmas(type, curConditions) // 获取通用参数
  Object.assign(params, pager || {}) // 参数
  let url = '' // 获取详细信息的url
  switch (type) {
    case 'evaluation': // '学生测评'
      url =
        '/api/blade-measure/measure/projectgaugeincludeuser/findProjectGaugeIncludeUserPage'
      params.testFlag = doneType === 'done' ? 'Y' : 'N' // 是否查询已完成的数据
      break
    case 'warn': // '学生预警'
      params.interveneFlag = doneType === 'done' ? 'Y' : 'N' // 是否查询已完成的数据
      url = '/api/blade-measure/measure/report/findUserWarnList'
      break
    case 'reserve': // '学生预约'
      url = '/api/blade-measure/measure/report/findUserWarnList' // ---------------测试数据---------------------------------
      break
  }
  let { data } = await axios({
    url: url,
    method: 'get',
    params: {
      tenantId: state.user.tenantId,
      specialTypeId: 1,
      ...params,
    },
  })
  return {
    data: data.success ? data.data.records || [] : [],
    total: data.success ? data.data.total : 0,
  }
}

// 获取参数
function getParmas(type: string, curConditions: any) {
  let params: any = Object.assign({}, curConditions || {}) // 参数
  switch (type) {
    case 'evaluation': // '学生测评'
      params['userName'] = params['name']
      params['includeName'] = params['name']
      break
    case 'warn': // '学生预警'
      params['userName'] = params['name']
      params['includeName'] = params['name']
      params.levelFlag = 'Y'
      break
    case 'reserve': // '学生预约'
      break
  }
  delete params['name']
  return params
}

// 根据角色、类型获取对应的显示字段
export let getColumns = function (
  userInfo: any, // 用户信息
  activeNames: any,
  type: any
) {
  if (!userInfo) {
    return []
  }
  let roleCode: string = getRoleCode(userInfo)
  let calssCode
  if (activeNames === 'evaluation') {
    calssCode = 'evaluation'
  } else if (activeNames === 'warn') {
    calssCode = 'warn'
  } else if (activeNames === 'reserve') {
    calssCode = 'reserve'
  } else {
    // 其他
    calssCode = 'other'
  }
  return getColumnsByRoleAndType(userInfo, roleCode, calssCode, type)
}

// 获取字段配置
function getColumnsByRoleAndType(
  userInfo: any,
  roleCode: string,
  calssCode: string,
  type: any
) {
  let data = [
    {
      columnOp: { field: 'includeName', label: '姓名', width: '70' }, // 字段配置
      roleCodes: ['student', 'parent', 'teacher', 'other'], // 哪些角色可展示
      calssCodes: ['evaluation'], // 哪些待办类型可展示
      types: ['undone', 'done'], // 哪些待办状态可展示
    },
    {
      columnOp: { field: 'userName', label: '姓名', width: '70' }, // 字段配置
      roleCodes: ['student', 'parent', 'teacher', 'other'], // 哪些角色可展示
      calssCodes: ['warn', 'reserve', 'other'], // 哪些待办类型可展示
      types: ['undone', 'done'], // 哪些待办状态可展示
    },
    {
      columnOp: { field: 'gradeName', label: '年级', width: '70' }, // 字段配置
      roleCodes: ['student', 'parent', 'teacher', 'other'], // 哪些角色可展示
      calssCodes: ['evaluation'], // 哪些待办类型可展示
      types: ['undone', 'done'], // 哪些待办状态可展示
    },
    {
      columnOp: { field: 'className', label: '班级', width: '70' }, // 字段配置
      roleCodes: ['student', 'parent', 'teacher', 'other'], // 哪些角色可展示
      calssCodes: ['evaluation', 'warn', 'reserve', 'other'], // 哪些待办类型可展示
      types: ['undone', 'done'], // 哪些待办状态可展示
    },
    {
      columnOp: {
        field: 'level',
        label: '预警等级',
        width: '65', // '70',
        type: 'warnLevel',
        enumKey: 'level',
      }, // 字段配置  预警等级
      roleCodes: ['student', 'parent', 'teacher', 'other'], // 哪些角色可展示
      calssCodes: ['warn'], // 哪些待办类型可展示
      types: ['undone', 'done'], // 哪些待办状态可展示
    },
  ]
  let columns: Array<object> = []
  data.forEach((d) => {
    // 按条件过滤，符合条件的才展示
    if (
      d['roleCodes'].includes(roleCode) &&
      d['calssCodes'].includes(calssCode) &&
      d['types'].includes(type)
    ) {
      columns.push(d['columnOp'])
    }
  })
  /** start 操作按钮特殊处理 */
  let { isShow } = getHandlePerm(userInfo, roleCode, calssCode, type)
  /** end 操作按钮特殊处理 */
  columns.push({
    field: 'projectName',
    label: '项目名称',
    width: 'auto',
    type: isShow ? 'link' : '',
  })
  if (isShow) {
    // columns.push({ field: '_handle_', label: '操作', type: 'btn', width: 70 }) // 字段配置，type: 'btn'=按钮
  }
  return columns
}
/**
 * 获取操作按钮的权限
 * @param roleCode
 * @param calssCode 待办类型
 * @param doneType 完成状态
 * @returns {
 *  isEdit: true, // 是否可编辑
 *  isShow: true // 是否可显示
 * }
 */
export const getHandlePerm = function (
  userInfo: any,
  roleCode: string,
  calssCode: string,
  doneType: string
) {
  let perm = {
    isEdit: false, // 是否可编辑
    isShow: false, // 是否可显示
  }
  // 是否心理老师/心理专家 （保护隐私，心理老师/心理专家才能查看学生测评报告与干预）
  let isXinli = (userInfo.postName || '').includes('心理')
  // 已完成
  if (doneType === 'done') {
    // 是否心理老师/心理专家
    if (isXinli) {
      perm.isShow = true // 可以查看测评或预警报告
    } else {
      perm.isShow = false // 不能查看报告
    }
    perm.isEdit = false
  } else {
    // 未完成
    // 测评类型
    if (calssCode === 'evaluation') {
      let isPerm = roleCode === 'student' // 是学生
      perm.isShow = isPerm
      perm.isEdit = isPerm
    }
    // 预警类型
    else if (calssCode === 'warn') {
      perm.isShow = isXinli
      perm.isEdit = isXinli
    }
    // 预约类型
    else if (calssCode === 'reserve') {
      let isPerm = roleCode === 'teacher' // 是老师
      perm.isShow = isPerm
      perm.isEdit = isPerm
    }
  }
  return perm
}
